<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *文章管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['tuisong']=array('1'=>'已推送','0'=>'未推送');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','group_id','title','image','desc','content','status','ishome','ishot','istoutiao','iswechat','paixu','likenum','likenum','createtime');
	$list=pdo_getslice('jy_cmsarticle_news',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['group_title']=pdo_getcolumn('jy_cmsarticle_newsgroup',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['group_id']),'title');

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'文章列表'),
			array('op'=>'edit','title'=>'添加文章'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'文章id','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'group_title','title'=>'分类','type'=>'string'),
			array('name'=>'istoutiao','title'=>'已推送头条号','type'=>'radio','options'=>'tuisong'),
			array('name'=>'iswechat','title'=>'已推送企鹅号','type'=>'radio','options'=>'tuisong'),
			//array('name'=>'ishome','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'是否显示','type'=>'switch','set'=>'set'),
		),
		'actions'=>array(
			array('type'=>'post','op'=>'toutiao','title'=>'推送到头条号','message'=>'确定推送该文章到头条号吗！','show'=>''),
			array('type'=>'post','op'=>'wechat','title'=>'推送到企鹅号','message'=>'确定推送该文章到企鹅号吗！','show'=>''),
			),
		),
		'search'=>array(
			array('name'=>'title','title'=>'文章标题','type'=>'string','width'=>'15'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cmsarticle_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回文章列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'名称','name'=>'title','help'=>''),
					array('type'=>'image','title'=>'图片','name'=>'image','help'=>''),
					array('name'=>'group_id','title'=>'所属分类','type'=>'select','options'=>'group_id','option_title'=>'title'),
					array('type'=>'string','title'=>'描述','name'=>'desc','help'=>''),
					array('type'=>'richtext','title'=>'内容','name'=>'content','help'=>''),
					array('type'=>'string','title'=>'点赞数量','name'=>'likenum','help'=>'虚拟点赞数量'),
					array('type'=>'string','title'=>'阅读数量','name'=>'readnum','help'=>'虚拟阅读数量'),
					array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
					//array('type'=>'switch','title'=>'是否首页','name'=>'ishome','help'=>''),
					array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>''),
					array('type'=>'switch','title'=>'是否展示','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['group_id']=pdo_getall('jy_cmsarticle_newsgroup',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
		if(empty($postdata['title'])){
		exi('请完善名称','error');
	}
	if(empty($postdata['image'])){
		exi('请完善图片','error');
	}
	if(!$postdata['group_id']){
		exi('请选择分类','error');
	}
	if(empty($postdata['content'])){
		exi('请完善内容','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'desc'=>trim($postdata['desc']),
		'content'=>trim($postdata['content']),
		'likenum'=>intval($postdata['likenum']),
		'group_id'=>intval($postdata['group_id']),
		'readnum'=>intval($postdata['readnum']),
		'paixu'=>intval($postdata['paixu']),
		'ishome'=>intval($postdata['ishome']),
		'ishot'=>intval($postdata['ishot']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_cmsarticle_news',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_cmsarticle_news',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_cmsarticle_news',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的文章！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_cmsarticle_news',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishome'])){
		pdo_update('jy_cmsarticle_news',array('ishome'=>intval($post['ishome'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_cmsarticle_news',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}

if($_RQ['op']=='toutiao'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要推送的文章！','error');
	}
	$news=pdo_get('jy_cmsarticle_news',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));  //文章详情
	$group_title=pdo_getcolumn('jy_cmsarticle_newsgroup',array('uniacid'=>$_SESSION['uniacid'],'id'=>$news['group_id']),'title');
	$data=cfc('jy_toutiao')->new_article($news['title'],$new['content'],'',1,$group_title);
	if($data['message']=='error'){
		exi('推送失败：'.$data['data'],'error');
	}
	exi('推送成功');
}
if($_RQ['op']=='wechat'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要推送的文章！','error');
	}
	$news=pdo_get('jy_cmsarticle_news',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));  //文章详情
	$data=cfc('jy_qier')->article($news['title'],$new['content'],$news['image']);
	if($data['msg']!='success'){
		exi('推送失败','error');
	}
	exi('推送成功');
}